<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. DEX文件简介 ######1). 基本格式">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-解析DEX文件">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android-Plugin/Android-解析DEX文件/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. DEX文件简介 ######1). 基本格式">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-f4d58c1cac638339.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-a785e75cc7e0ab0c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-16190b82cacf4cac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:11:11.818Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-解析DEX文件">
<meta name="twitter:description" content="####1. DEX文件简介 ######1). 基本格式">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-f4d58c1cac638339.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android-Plugin/Android-解析DEX文件/"/>





  <title>Android-解析DEX文件 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android-Plugin/Android-解析DEX文件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-解析DEX文件</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. DEX文件简介</p>
<p>######1). 基本格式<br><img src="https://upload-images.jianshu.io/upload_images/3110861-f4d58c1cac638339.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Android DEX文件格式--非虫大神杰作.png"></p>
<a id="more"></a>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-a785e75cc7e0ab0c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="dex-file-general-structure.png"></p>
<ul>
<li>Dex Header: header文件头</li>
<li>String Table: 字符串的索引</li>
<li>Type Table: 类型的索引</li>
<li>Proto Table: 方法原型的索引</li>
<li>Field Table: 域的索引</li>
<li>Method Table: 方法索引</li>
<li>Class Def Table: 类的定义区</li>
<li>Data Section: 数据区</li>
</ul>
<p>######1). Dex Header<br>dex文件里的header，描述.dex文件的文件信息，及其它各个区域的索引。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * DEX 头部信息类型</span><br><span class="line"> * 主要分为两部分：</span><br><span class="line"> * 	1). 魔数 + 签名 + 文件大小等信息</span><br><span class="line"> * 	2). 后面的各个数据结构的大小和偏移值，成对出现</span><br><span class="line"> * </span><br><span class="line"> * struct DexHeader &#123;</span><br><span class="line">	    u1  magic[8];           // includes version number</span><br><span class="line">	    u4  checksum;           // adler32 checksum </span><br><span class="line">	    u1  signature[kSHA1DigestLen]; // SHA-1 hash</span><br><span class="line">	    u4  fileSize;           // length of entire file</span><br><span class="line">	    u4  headerSize;         // offset to start of next section</span><br><span class="line">	    u4  endianTag;</span><br><span class="line">	    u4  linkSize;</span><br><span class="line">	    u4  linkOff;</span><br><span class="line">	    u4  mapOff;</span><br><span class="line">	    u4  stringIdsSize;</span><br><span class="line">	    u4  stringIdsOff;</span><br><span class="line">	    u4  typeIdsSize;</span><br><span class="line">	    u4  typeIdsOff;</span><br><span class="line">	    u4  protoIdsSize;</span><br><span class="line">	    u4  protoIdsOff;</span><br><span class="line">	    u4  fieldIdsSize;</span><br><span class="line">	    u4  fieldIdsOff;</span><br><span class="line">	    u4  methodIdsSize;</span><br><span class="line">	    u4  methodIdsOff;</span><br><span class="line">	    u4  classDefsSize;</span><br><span class="line">	    u4  classDefsOff;</span><br><span class="line">	    u4  dataSize;</span><br><span class="line">	    u4  dataOff;</span><br><span class="line">	&#125;;</span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class HeaderType &#123;</span><br><span class="line">	/**8个字节，一般是常量，为使.dex文件能够被识别出来，必须出现在.dex文件的最开头位置</span><br><span class="line">	 *	数组的值一般可以转换为一个字符串： &#123; 0x64 0x65 0x78 0x0a 0x30 0x33 0x35 0x00 &#125; = &quot;dex\n035\0&quot;</span><br><span class="line">	 *	中间是一个 ‘\n&apos; 符号 ，后面 035 是 Dex 文件格式的版本 。</span><br><span class="line">	 */</span><br><span class="line">	public byte[] magic = new byte[8];</span><br><span class="line">	/**文件校验码，使用alder32算法校验文件出去magic，checknum外余下所有文件区域用于检查文件错误*/</span><br><span class="line">	public int checkSum;</span><br><span class="line">	/**采用SHA-1算法hash出去magic,checknum和signature外余下所有的文件区域，用于唯一识别本文件*/</span><br><span class="line">	public byte[] signAture = new byte[20];</span><br><span class="line">	/**DEX文件的大小*/</span><br><span class="line">	public int fileSize;</span><br><span class="line">	/**header区域的大小，单位Byte，一般固定为0x70常量*/</span><br><span class="line">	public int headerSize;</span><br><span class="line">	/**大小端标签，标准.dex文件为小端，此项一般固定为0x12345678常量*/</span><br><span class="line">	public int endianTag;</span><br><span class="line">	/**链接数据的大小*/</span><br><span class="line">	public int linkSize;</span><br><span class="line">	/**链接数据的偏移值*/</span><br><span class="line">	public int linkOff;</span><br><span class="line">	/**map item的偏移地址，该item属于data区里的内容，值要大于等于dataOff的大小*/</span><br><span class="line">	public int mapOff;</span><br><span class="line">	/**DEX中用到的所有字符串内容的大小*/</span><br><span class="line">	public int stringIdsSize;</span><br><span class="line">	/**DEX中用到的所有字符串内容的偏移量*/</span><br><span class="line">	public int stringIdsOff;</span><br><span class="line">	/**DEX中类型数据结构的大小*/</span><br><span class="line">	public int typeIdsSize;</span><br><span class="line">	/**DEX中类型数据结构的偏移值*/</span><br><span class="line">	public int typeIdsOff;</span><br><span class="line">	/**DEX中的元数据信息数据结构的大小*/</span><br><span class="line">	public int protoIdsSize;</span><br><span class="line">	/**DEX中的元数据信息数据结构的偏移值*/</span><br><span class="line">	public int protoIdsOff;</span><br><span class="line">	/**DEX中字段信息数据结构的大小*/</span><br><span class="line">	public int fieldIdsSize;</span><br><span class="line">	/**DEX中字段信息数据结构的偏移值*/</span><br><span class="line">	public int fieldIdsOff;</span><br><span class="line">	/**DEX中方法信息数据结构的大小*/</span><br><span class="line">	public int methodIdsSize;</span><br><span class="line">	/**DEX中方法信息数据结构的偏移值*/</span><br><span class="line">	public int methodIdsOff;</span><br><span class="line">	/**DEX中的类信息数据结构的大小*/</span><br><span class="line">	public int classDefsSize;</span><br><span class="line">	/**DEX中的类信息数据结构的偏移值*/</span><br><span class="line">	public int classDefsOff;</span><br><span class="line">	/**DEX中数据区域的结构信息的大小*/</span><br><span class="line">	public int dataSize;</span><br><span class="line">	/**DEX中数据区域的结构信息的偏移值*/</span><br><span class="line">	public int dataOff;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString()&#123;</span><br><span class="line">		return &quot;magic:&quot;+Util.bytesToHexString(magic)+&quot;\n&quot;</span><br><span class="line">				+ &quot;checksum:&quot;+checkSum + &quot;\n&quot;</span><br><span class="line">				+ &quot;siganature:&quot;+Util.bytesToHexString(signAture) + &quot;\n&quot;</span><br><span class="line">				+ &quot;file_size:&quot;+fileSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;header_size:&quot;+headerSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;endian_tag:&quot;+endianTag + &quot;\n&quot;</span><br><span class="line">				+ &quot;link_size:&quot;+linkSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;link_off:&quot;+Util.bytesToHexString(Util.int2Byte(linkOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;map_off:&quot;+Util.bytesToHexString(Util.int2Byte(mapOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;string_ids_size:&quot;+stringIdsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;string_ids_off:&quot;+Util.bytesToHexString(Util.int2Byte(stringIdsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;type_ids_size:&quot;+typeIdsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;type_ids_off:&quot;+Util.bytesToHexString(Util.int2Byte(typeIdsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;proto_ids_size:&quot;+protoIdsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;proto_ids_off:&quot;+Util.bytesToHexString(Util.int2Byte(protoIdsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;field_ids_size:&quot;+fieldIdsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;field_ids_off:&quot;+Util.bytesToHexString(Util.int2Byte(fieldIdsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;method_ids_size:&quot;+methodIdsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;method_ids_off:&quot;+Util.bytesToHexString(Util.int2Byte(methodIdsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;class_defs_size:&quot;+classDefsSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;class_defs_off:&quot;+Util.bytesToHexString(Util.int2Byte(classDefsOff)) + &quot;\n&quot;</span><br><span class="line">				+ &quot;data_size:&quot;+dataSize + &quot;\n&quot;</span><br><span class="line">				+ &quot;data_off:&quot;+Util.bytesToHexString(Util.int2Byte(dataOff));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-16190b82cacf4cac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<p>######2). String Table<br>string_ids区索引.dex文件所有的字符串<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 索引.dex文件所有的字符串</span><br><span class="line"> * </span><br><span class="line"> * struct DexStringId &#123;</span><br><span class="line">    	u4 stringDataOff;      // file offset to string_data_item</span><br><span class="line">	&#125;;</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class StringIdsItem &#123;</span><br><span class="line">	/**字符串数据的偏移地址*/</span><br><span class="line">	public int stringDataOff;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 获取当前类属性所占字节大小</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 4;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return Util.bytesToHexString(Util.int2Byte(stringDataOff));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>string_data_off是一个偏移地址，指向的数据结构为string_data_item.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * StringIdsItem中stringDataOff指向的数据结构</span><br><span class="line"> * struct string_data_item &#123;</span><br><span class="line">		uleb128 utf16_size;</span><br><span class="line">		ubyte data;</span><br><span class="line">	&#125;</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class StringDataItem &#123;</span><br><span class="line">	/**LEB128 （ little endian base 128 ) 格式 ，是基于 1 个 Byte 的一种不定长度的</span><br><span class="line">		编码方式 。若第一个 Byte 的最高位为 1 ，则表示还需要下一个 Byte 来描述 ，直至最后一个 Byte 的最高</span><br><span class="line">		位为 0 。每个 Byte 的其余 Bit 用来表示数据*/</span><br><span class="line">	public byte data;</span><br><span class="line">	public List&lt;Byte&gt; utf16Size = new ArrayList&lt;&gt;();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). Type Table<br>主要描述dex中所有的类型，如类类型，基本类型等信息，type_ids区索引了dex文件里的所有数据类型，包括class类型，数据类型(array types) 和基本类型(primitive types).<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * DEX所有类型，基本类型等信息</span><br><span class="line"> * type_ids 区索引了 dex 文件里的所有数据类型 ，包括 class 类型 ，数组类型（array types）和基本类型(primitive types)</span><br><span class="line"> * </span><br><span class="line"> * struct DexTypeId &#123;</span><br><span class="line">    u4  descriptorIdx;      // index into stringIds list for type descriptor </span><br><span class="line">	&#125;;</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class TypeIdsItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * ID索引</span><br><span class="line">	 */</span><br><span class="line">	public int descriptorIdx;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 获取所属</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 4;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######4). Proto Table<br>proto是method prototype代表java语言里的一个method的原型<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * proto 的意思是 method prototype 代表 java 语言里的一个 method 的原型 。proto_ids 里的元素为 proto_id_item ,</span><br><span class="line"> * </span><br><span class="line"> * struct DexProtoId &#123;</span><br><span class="line">	    u4  shortyIdx;          // index into stringIds for shorty descriptor</span><br><span class="line">	    u4  returnTypeIdx;      // index into typeIds list for return type </span><br><span class="line">	    u4  parametersOff;      // file offset to type_list for parameter types</span><br><span class="line">	&#125;;</span><br><span class="line"> *</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ProtoIdsItem &#123;</span><br><span class="line">	/**值为一个string_ids的index号，用来说明该method原型*/</span><br><span class="line">	public int shortyIdx;</span><br><span class="line">	/**值为一个type_ids的index，表示该method原型的返回值类型*/</span><br><span class="line">	public int returnTypeIdx;</span><br><span class="line">	/**指定method原型的参数列表type_list，若method没有参数，则值为0. 参数的格式是type_list*/</span><br><span class="line">	public int parametersOff;</span><br><span class="line">	</span><br><span class="line">	// 这个不是公共字段，而是为了存储方法原型中的参数类型名和参数个数</span><br><span class="line">	public List&lt;String&gt; paramtersList = new ArrayList&lt;&gt;();</span><br><span class="line">	public int paramterCount;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 获取当前类字节数</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 4 + 4 + 4;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;shortyIdx:&quot;+shortyIdx+&quot;,returnTypeIdx:&quot;+returnTypeIdx+&quot;,parametersOff:&quot;+parametersOff;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5). Field Table<br>field_ids区里面存放的是dex文件引用的所有field<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * field_ids区里存放的是dex文件引用的所有的field，本区元素格式是field_id_item</span><br><span class="line"> * </span><br><span class="line"> * struct DexFieldId &#123;</span><br><span class="line">	    u2  classIdx;           // index into typeIds list for defining class</span><br><span class="line">	    u2  typeIdx;            // index into typeIds for field type</span><br><span class="line">	    u4  nameIdx;            // index into stringIds for field name</span><br><span class="line">	&#125;;</span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class FieldIdsItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * field所属的class类型，class_idx的值时type_ids的一个index，指向所属的类</span><br><span class="line">	 */</span><br><span class="line">	public short classIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * field的类型，值是type_ids的一个index</span><br><span class="line">	 */</span><br><span class="line">	public short typeIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * field的名称，它的值是string_ids的一个index</span><br><span class="line">	 */</span><br><span class="line">	public int nameIdx;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 当前区域所占字节大小</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 2 + 2 + 4;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;classIdx: &quot; + classIdx + &quot;,typeIdx: &quot; + typeIdx + &quot;,nameIdx: &quot; + nameIdx;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######6). Method Table<br>method_ids是索引区的最后一个条目，它索引了dex文件里的所有的method.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * method_ids是索引区的最后一个条目，它索引了dex文件里的所有method.</span><br><span class="line"> * method_ids的元素格式是method_id_item</span><br><span class="line"> * </span><br><span class="line"> * struct DexMethodId &#123;</span><br><span class="line">	    u2  classIdx;           // index into typeIds list for defining class</span><br><span class="line">	    u2  protoIdx;           // index into protoIds for method prototype</span><br><span class="line">	    u4  nameIdx;            // index into stringIds for method name</span><br><span class="line">	&#125;;</span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class MethodIdsItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * method所属的class类型，class_idx的值是type_ids的一个index，必须指向一个class类型</span><br><span class="line">	 */</span><br><span class="line">	public short classIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * method的原型，指向proto_ids的一个index</span><br><span class="line">	 */</span><br><span class="line">	public short protoIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * method的名称，值为string_ids的一个index</span><br><span class="line">	 */</span><br><span class="line">	public int nameIdx;</span><br><span class="line">	</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 2 + 2 + 4;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String toString()&#123;</span><br><span class="line">		return &quot;classIdx: &quot; + classIdx + &quot;,protoIdx: &quot; + protoIdx + &quot;,nameIdx: &quot; + nameIdx;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######7). Class Def Table<br>class_defs区域里存放着class definitions，class的定义。它的结构较dex区要复杂，有的数据直接指向data区<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * class_defs区域里存放着class definitions，有些数据直接指向了data区</span><br><span class="line"> * </span><br><span class="line"> * struct DexClassDef &#123;</span><br><span class="line">	    u4  classIdx;           /* index into typeIds for this class</span><br><span class="line">	    u4  accessFlags;</span><br><span class="line">	    u4  superclassIdx;      /* index into typeIds for superclass</span><br><span class="line">	    u4  interfacesOff;      /* file offset to DexTypeList</span><br><span class="line">	    u4  sourceFileIdx;      /* index into stringIds for source file name</span><br><span class="line">	    u4  annotationsOff;     /* file offset to annotations_directory_item</span><br><span class="line">	    u4  classDataOff;       /* file offset to class_data_item</span><br><span class="line">	    u4  staticValuesOff;    /* file offset to DexEncodedArray</span><br><span class="line">	&#125;;</span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ClassDefItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 描述具体的class类型，值是type_ids的一个index，值必须是一个class类型，不能是数组雷兴国或者基本类型</span><br><span class="line">	 */</span><br><span class="line">	public int classIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * 描述class的访问类型，如public,final,static等</span><br><span class="line">	 */</span><br><span class="line">	public int accessFlags;</span><br><span class="line">	/**</span><br><span class="line">	 * 描述父类的类型，值必须是一个class类型，不能是数组雷兴国或者基本类型</span><br><span class="line">	 */</span><br><span class="line">	public int superClassIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * 值为偏移地址，被指向的数据结构为type_list，class若没有interfaces，值为0</span><br><span class="line">	 */</span><br><span class="line">	public int interfacesOff;</span><br><span class="line">	/**</span><br><span class="line">	 * 表示源代码文件的信息，值为string_ids的一个index。若此项信息丢失，此项赋值为NO_INDEX=0xFFFFFFFF</span><br><span class="line">	 */</span><br><span class="line">	public int sourceFileIdx;</span><br><span class="line">	/**</span><br><span class="line">	 * 值为偏移地址，指向的内容是该class的注解，位置在data区，格式为annotations_directory_item，若没有此项，值为0</span><br><span class="line">	 */</span><br><span class="line">	public int annotationsOff;</span><br><span class="line">	/**</span><br><span class="line">	 * 值为偏移地址，指向的内容是该class的使用到的数据，位置在data区，格式为class_data_item。无偶没有此项，则值为0</span><br><span class="line">	 */</span><br><span class="line">	public int classDataOff;</span><br><span class="line">	/**</span><br><span class="line">	 * 值为偏移地址，指向data区里的一个列表，格式为encoded_array_item。若没有此项，值为0.</span><br><span class="line">	 */</span><br><span class="line">	public int staticValueOff;</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * enum &#123;</span><br><span class="line">		    ACC_PUBLIC       = 0x00000001,       // class, field, method, ic</span><br><span class="line">		    ACC_PRIVATE      = 0x00000002,       // field, method, ic</span><br><span class="line">		    ACC_PROTECTED    = 0x00000004,       // field, method, ic</span><br><span class="line">		    ACC_STATIC       = 0x00000008,       // field, method, ic</span><br><span class="line">		    ACC_FINAL        = 0x00000010,       // class, field, method, ic</span><br><span class="line">		    ACC_SYNCHRONIZED = 0x00000020,       // method (only allowed on natives)</span><br><span class="line">		    ACC_SUPER        = 0x00000020,       // class (not used in Dalvik)</span><br><span class="line">		    ACC_VOLATILE     = 0x00000040,       // field</span><br><span class="line">		    ACC_BRIDGE       = 0x00000040,       // method (1.5)</span><br><span class="line">		    ACC_TRANSIENT    = 0x00000080,       // field</span><br><span class="line">		    ACC_VARARGS      = 0x00000080,       // method (1.5)</span><br><span class="line">		    ACC_NATIVE       = 0x00000100,       // method</span><br><span class="line">		    ACC_INTERFACE    = 0x00000200,       // class, ic</span><br><span class="line">		    ACC_ABSTRACT     = 0x00000400,       // class, method, ic</span><br><span class="line">		    ACC_STRICT       = 0x00000800,       // method</span><br><span class="line">		    ACC_SYNTHETIC    = 0x00001000,       // field, method, ic</span><br><span class="line">		    ACC_ANNOTATION   = 0x00002000,       // class, ic (1.5)</span><br><span class="line">		    ACC_ENUM         = 0x00004000,       // class, field, ic (1.5)</span><br><span class="line">		    ACC_CONSTRUCTOR  = 0x00010000,       // method (Dalvik only)</span><br><span class="line">		    ACC_DECLARED_SYNCHRONIZED =</span><br><span class="line">		                       0x00020000,       // method (Dalvik only)</span><br><span class="line">		    ACC_CLASS_MASK =</span><br><span class="line">		        (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT</span><br><span class="line">		                | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),</span><br><span class="line">		    ACC_INNER_CLASS_MASK =</span><br><span class="line">		        (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),</span><br><span class="line">		    ACC_FIELD_MASK =</span><br><span class="line">		        (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL</span><br><span class="line">		                | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),</span><br><span class="line">		    ACC_METHOD_MASK =</span><br><span class="line">		        (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL</span><br><span class="line">		                | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE</span><br><span class="line">		                | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR</span><br><span class="line">		                | ACC_DECLARED_SYNCHRONIZED),</span><br><span class="line">		&#125;;</span><br><span class="line"></span><br><span class="line">	 */</span><br><span class="line">	</span><br><span class="line">	/**</span><br><span class="line">	 * 访问修饰符</span><br><span class="line">	 */</span><br><span class="line">	public static final int </span><br><span class="line">		ACC_PUBLIC       = 0x00000001,       // class, field, method, ic</span><br><span class="line">	    ACC_PRIVATE      = 0x00000002,       // field, method, ic</span><br><span class="line">	    ACC_PROTECTED    = 0x00000004,       // field, method, ic</span><br><span class="line">	    ACC_STATIC       = 0x00000008,       // field, method, ic</span><br><span class="line">	    ACC_FINAL        = 0x00000010,       // class, field, method, ic</span><br><span class="line">	    ACC_SYNCHRONIZED = 0x00000020,       // method (only allowed on natives)</span><br><span class="line">	    ACC_SUPER        = 0x00000020,       // class (not used in Dalvik)</span><br><span class="line">	    ACC_VOLATILE     = 0x00000040,       // field</span><br><span class="line">	    ACC_BRIDGE       = 0x00000040,       // method (1.5)</span><br><span class="line">	    ACC_TRANSIENT    = 0x00000080,       // field</span><br><span class="line">	    ACC_VARARGS      = 0x00000080,       // method (1.5)</span><br><span class="line">	    ACC_NATIVE       = 0x00000100,       // method</span><br><span class="line">	    ACC_INTERFACE    = 0x00000200,       // class, ic</span><br><span class="line">	    ACC_ABSTRACT     = 0x00000400,       // class, method, ic</span><br><span class="line">	    ACC_STRICT       = 0x00000800,       // method</span><br><span class="line">	    ACC_SYNTHETIC    = 0x00001000,       // field, method, ic</span><br><span class="line">	    ACC_ANNOTATION   = 0x00002000,       // class, ic (1.5)</span><br><span class="line">	    ACC_ENUM         = 0x00004000,       // class, field, ic (1.5)</span><br><span class="line">	    ACC_CONSTRUCTOR  = 0x00010000,       // method (Dalvik only)</span><br><span class="line">	    ACC_DECLARED_SYNCHRONIZED =</span><br><span class="line">	                       0x00020000,       // method (Dalvik only)</span><br><span class="line">	    ACC_CLASS_MASK =</span><br><span class="line">	        (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT</span><br><span class="line">	                | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),</span><br><span class="line">	    ACC_INNER_CLASS_MASK =</span><br><span class="line">	        (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),</span><br><span class="line">	    ACC_FIELD_MASK =</span><br><span class="line">	        (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL</span><br><span class="line">	                | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),</span><br><span class="line">	    ACC_METHOD_MASK =</span><br><span class="line">	        (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL</span><br><span class="line">	                | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE</span><br><span class="line">	                | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR</span><br><span class="line">	                | ACC_DECLARED_SYNCHRONIZED);</span><br><span class="line"></span><br><span class="line">	/**</span><br><span class="line">	 * 获取当前区域所占字节数</span><br><span class="line">	 * @return</span><br><span class="line">	 */</span><br><span class="line">	public static int getSize() &#123;</span><br><span class="line">		return 4 * 8;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString()&#123;</span><br><span class="line">		return &quot;classIdx: &quot; + classIdx + &quot;,accessFlags: &quot; + accessFlags + &quot;,superClassIdx: &quot; + superClassIdx + </span><br><span class="line">				&quot;,iterfacesOff: &quot; + interfacesOff + &quot;,sourceFileIdx: &quot; + sourceFileIdx + &quot;,annotationsOff: &quot; + </span><br><span class="line">				annotationsOff + &quot;,classDataOff: &quot; + classDataOff + &quot;,staticValueOff: &quot; + staticValueOff;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>class_data_off指向data区里的class_data_itetm结构，class_data_item里存放着本class使用到的各种数据.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * class_data_off指向data区里的class_data_item结构，class_data_item里存放着本class使用到的各种数据</span><br><span class="line"> * </span><br><span class="line"> * 	struct class_data_item	&#123;</span><br><span class="line">		uleb128 static_fields_size;</span><br><span class="line">		uleb128 instance_fields_size;</span><br><span class="line">		uleb128 direct_methods_size;</span><br><span class="line">		uleb128 virtual_methods_size;</span><br><span class="line">		encoded_field static_fields [ static_fields_size ];</span><br><span class="line">		encoded_field instance_fields [ instance_fields_size ];</span><br><span class="line">		encoded_method direct_methods [ direct_method_size ];</span><br><span class="line">		encoded_method virtual_methods [ virtual_methods_size ];</span><br><span class="line">	&#125;;</span><br><span class="line"></span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class ClassDataItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 静态字段的大小</span><br><span class="line">	 */</span><br><span class="line">	public int staticFieldsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 实例化字段的大小</span><br><span class="line">	 */</span><br><span class="line">	public int instanceFieldsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 实现方法的大小</span><br><span class="line">	 */</span><br><span class="line">	public int directMethodsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 虚拟方法的大小</span><br><span class="line">	 */</span><br><span class="line">	public int virtualMethodsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 静态字段数组</span><br><span class="line">	 */</span><br><span class="line">	public EncodeField[] staticFields;</span><br><span class="line">	/**</span><br><span class="line">	 * 实例化字段数组</span><br><span class="line">	 */</span><br><span class="line">	public EncodeField[] instanceFields;</span><br><span class="line">	/**</span><br><span class="line">	 * 实现方法数组</span><br><span class="line">	 */</span><br><span class="line">	public EncodeMethod[] directMethods;</span><br><span class="line">	/**</span><br><span class="line">	 * 虚拟方法的数组</span><br><span class="line">	 */</span><br><span class="line">	public EncodeMethod[] virtualMethods;</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString()&#123;</span><br><span class="line">		return &quot;staticFieldsSize: &quot; + staticFieldsSize + &quot;,instanceFieldsSize: &quot; + instanceFieldsSize</span><br><span class="line">				+ &quot;,directMethodsSize:&quot; + directMethodsSize + &quot;,virtualMethodsSize: &quot; + virtualMethodsSize</span><br><span class="line">				+ &quot;\n&quot; + getFieldsAndMethods();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	private String getFieldsAndMethods() &#123;</span><br><span class="line">		StringBuilder sb = new StringBuilder();</span><br><span class="line">		sb.append(&quot;staticFields:\n&quot;);</span><br><span class="line">		for(int i=0;i&lt;staticFields.length;i++)&#123;</span><br><span class="line">			sb.append(staticFields[i]+&quot;\n&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		sb.append(&quot;instanceFields:\n&quot;);</span><br><span class="line">		for(int i=0;i&lt;instanceFields.length;i++)&#123;</span><br><span class="line">			sb.append(instanceFields[i]+&quot;\n&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		sb.append(&quot;directMethods:\n&quot;);</span><br><span class="line">		for(int i=0;i&lt;directMethods.length;i++)&#123;</span><br><span class="line">			sb.append(directMethods[i]+&quot;\n&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		sb.append(&quot;virtualMethods:\n&quot;);</span><br><span class="line">		for(int i=0;i&lt;virtualMethods.length;i++)&#123;</span><br><span class="line">			sb.append(virtualMethods[i]+&quot;\n&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		return sb.toString();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######8). Data Section<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 代码条目</span><br><span class="line"> * </span><br><span class="line"> * 	(1) 一个 .dex 文件被分成了 9 个区 ，其中有一个索引区叫做class_defs ， 索引了 .dex 里面用到的 class ，以及对这个 class 的描述 。</span><br><span class="line"> *	(2) class_defs 区 ， 里面其实是class_def_item 结构 。这个结构里描述了 LHello; 的各种信息 ，诸如名称 ，superclass , access flag，</span><br><span class="line"> *		 interface 等 。class_def_item 里有一个元素 class_data_off , 指向data 区里的一个 class_data_item 结构 ，</span><br><span class="line"> *		 用来描述 class 使用到的各种数据 。自此以后的结构都归于 data区了 。</span><br><span class="line"> *	(3) class_data_item 结构 ，里描述值着 class 里使用到的 static field , instance field , direct_method ，和 virtual_method </span><br><span class="line"> *		的数目和描述 。例子 Hello.dex 里 ，只有 2 个 direct_method , 其余的 field 和method 的数目都为 0 。描述 direct_method 的结构叫</span><br><span class="line"> *		做 encoded_method ，是用来详细描述某个 method的 。</span><br><span class="line"> *	(4) encoded_method 结构 ，描述某个 method 的 method 类型 ， access flags 和一个指向 code_item的偏移地址 ，里面存放的是该 method </span><br><span class="line"> *		的具体实现 。</span><br><span class="line"> *	(5) code_item ，结构里描述着某个 method 的具体实现 。</span><br><span class="line"> * </span><br><span class="line"> * struct DexCode &#123;</span><br><span class="line">	    u2  registersSize;</span><br><span class="line">	    u2  insSize;</span><br><span class="line">	    u2  outsSize;</span><br><span class="line">	    u2  triesSize;</span><br><span class="line">	    u4  debugInfoOff;       /* file offset to debug info stream</span><br><span class="line">	    u4  insnsSize;          /* size of the insns array, in u2 units</span><br><span class="line">	    u2  insns[1];</span><br><span class="line">	    /* followed by optional u2 padding</span><br><span class="line">	    /* followed by try_item[triesSize]</span><br><span class="line">	    /* followed by uleb128 handlersSize</span><br><span class="line">	    /* followed by catch_handler_item[handlersSize]</span><br><span class="line">	&#125;;</span><br><span class="line"> * </span><br><span class="line"> * @author mazaiting</span><br><span class="line"> *</span><br><span class="line"> */</span><br><span class="line">public class CodeItem &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 本段代码使用到的寄存器数目</span><br><span class="line">	 */</span><br><span class="line">	public short registersSize;</span><br><span class="line">	/**</span><br><span class="line">	 * method传入参数的数目</span><br><span class="line">	 */</span><br><span class="line">	public short insSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 本段代码调用其他方法时需要的参数个数</span><br><span class="line">	 */</span><br><span class="line">	public short outsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * try_item结构的个数</span><br><span class="line">	 */</span><br><span class="line">	public short triesSize;</span><br><span class="line">	/**</span><br><span class="line">	 * 偏移地址，指向本段代码的debug信息存放位置，是一个debug_info_item结构</span><br><span class="line">	 */</span><br><span class="line">	public int debugInfoOff;</span><br><span class="line">	/**</span><br><span class="line">	 * 指令列表的大小，以16-bit为单位。insns是instructions的缩写</span><br><span class="line">	 */</span><br><span class="line">	public int insnsSize;</span><br><span class="line">	/**</span><br><span class="line">	 * insns数组</span><br><span class="line">	 */</span><br><span class="line">	public short[] insns;</span><br><span class="line">	</span><br><span class="line">	/**tries和handlers用于处理java中的exception*/</span><br><span class="line">	</span><br><span class="line">	@Override</span><br><span class="line">	public String toString()&#123;</span><br><span class="line">		return &quot;registersSize: &quot; + registersSize + &quot;,insSize: &quot; + insSize + &quot;,outsSize: &quot; + outsSize + </span><br><span class="line">				&quot;,triesSize: &quot; + triesSize + &quot;,debugInfoOff: &quot; + debugInfoOff + &quot;,insnsSize: &quot; + insnsSize </span><br><span class="line">				+ &quot;\ninsns: &quot; + getInsnsStr();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private String getInsnsStr()&#123;</span><br><span class="line">		StringBuilder sb = new StringBuilder();</span><br><span class="line">		if (insns != null &amp;&amp; insns.length &gt; 0) &#123;</span><br><span class="line">			for(int i=0;i&lt;insns.length;i++)&#123;</span><br><span class="line">				sb.append(Util.bytesToHexString(Util.short2Byte(insns[i]))+&quot;,&quot;);</span><br><span class="line">			&#125;	</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		return sb.toString();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>####参考文章<br><a href="https://blog.csdn.net/jiangwei0910410003/article/details/50668549" target="_blank" rel="noopener">Android逆向之旅—解析编译之后的Dex文件格式</a></p>
<p>####<a href="https://gitee.com/mazaiting/app_protection/tree/master/ParseDEX" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android-Plugin/Android-解析AndroidManifest-xml文件/" rel="next" title="Android-解析AndroidManifest-xml文件">
                <i class="fa fa-chevron-left"></i> Android-解析AndroidManifest-xml文件
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android-Plugin/Android-解析resources-arsc文件/" rel="prev" title="Android-解析resources-arsc文件">
                Android-解析resources-arsc文件 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
